using System;

namespace PhillyCodeCamp.Domain.Validation
{
    [AttributeUsage(AttributeTargets.Property)]
    public class StringLengthValidatorAttribute : ValidationAttribute
    {
        private readonly int _minLength;
        private readonly int _maxLength;

        public StringLengthValidatorAttribute(int min, int max)
            : this(min, max, "Field length invalid")
        {
        }

        public StringLengthValidatorAttribute(int min, int max, string errMessage)
        {
            _minLength = min;
            _maxLength = max;
            _message = errMessage;
        }

        protected override bool validate(object target, object rawValue)
        {
            if (rawValue == null)
            {
                return true;
            }
            return (rawValue.ToString().Length >= _minLength && rawValue.ToString().Length <= _maxLength);
        }
    }
}